package com.cszjzx.member.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.cszjzx.member.po.Department;
import com.cszjzx.member.po.Field;
import com.cszjzx.member.po.Major;
import com.cszjzx.member.po.Master;
import com.cszjzx.member.po.Member;
import com.cszjzx.member.po.Stage;
import com.cszjzx.member.service.DepartmentService;
import com.cszjzx.member.service.DepartmentServiceImpl;
import com.cszjzx.member.service.MajorService;
import com.cszjzx.member.service.MajorServiceImpl;
import com.cszjzx.member.service.MemberService;
import com.cszjzx.member.service.MemberServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ScanAction extends ActionSupport {
	private int process;
	private int departmentId;
	private int majorId;
	private String idcard;
	private List<Department> departments;
	private List<Major> majors;
	private List<Member> members;
	private Member member;
	private String departmentName;
	private String majorName;
	private String stageName;
	private String[] s,s1,s2,s3,s4;
	private int number = 0 ;						//总的人数
	
	
	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public String[] getS() {
		return s;
	}

	public void setS(String[] s) {
		this.s = s;
	}

	public String[] getS1() {
		return s1;
	}

	public void setS1(String[] s1) {
		this.s1 = s1;
	}

	public String[] getS2() {
		return s2;
	}

	public void setS2(String[] s2) {
		this.s2 = s2;
	}

	public String[] getS3() {
		return s3;
	}

	public void setS3(String[] s3) {
		this.s3 = s3;
	}

	public String[] getS4() {
		return s4;
	}

	public void setS4(String[] s4) {
		this.s4 = s4;
	}
	
	DepartmentService departmentservice = new DepartmentServiceImpl();
	MajorService majorservice = new MajorServiceImpl();
	MemberService memberservice = new MemberServiceImpl();
	HttpServletRequest request = ServletActionContext.getRequest();
	
	
	public String getMajorName() {
		return majorName;
	}

	public String getStageName() {
		return stageName;
	}

	public void setStageName(String stageName) {
		this.stageName = stageName;
	}

	public void setMajorName(String majorName) {
		this.majorName = majorName;
	}

	public String getDepartmentName() {
		return departmentName;
	}

	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public List<Member> getMembers() {
		return members;
	}

	public void setMembers(List<Member> members) {
		this.members = members;
	}

	public List<Department> getDepartments() {
		return departments;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	public List<Major> getMajors() {
		return majors;
	}

	public void setMajors(List<Major> majors) {
		this.majors = majors;
	}

	public int getMajorId() {
		return majorId;
	}

	public void setMajorId(int majorId) {
		this.majorId = majorId;
	}

	public String getIdcard() {
		return idcard;
	}

	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}

	public int getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(int departmentId) {
		this.departmentId = departmentId;
	}

	public int getProcess() {
		return process;
	}

	public void setProcess(int process) {
		this.process = process;
	}
	
	
	
	public String school() throws Exception{
		Master m = (Master)request.getSession().getAttribute("master");
		int masterDepId = m.getDepartmentid();
		departments = departmentservice.getDepartmentsByProcess(Integer.toString(process),masterDepId);
		for(int i=0;i<departments.size();i++){
			number += departments.get(i).getNums();
		}
		return "showOfSchool";
	}
	
	public String department() throws Exception{
		majors = majorservice.getMajorsByDeapartmentAndProcess(departmentId, departmentName, Integer.toString(process));
		for(int i=0;i<majors.size();i++){
			number += majors.get(i).getNums();
		}
		return "showOfDepartment";
		
	}
	
	public String major() throws Exception{	
		members = memberservice.getMembersByProcessAndMajor(process,majorName);
		number = members.size();
		return "showOfMajor";
	}
	
	@SuppressWarnings("unchecked")
	public String person() throws Exception{
		member = memberservice.getMemberByIdcard(idcard);
		List<Stage> stages = (List<Stage>) request.getSession().getAttribute("stages");
		List<Field> fields = (List<Field>) request.getSession().getAttribute("fields");
		s = new String[stages.size()];
		int a1=0,a2=0,a3=0,a4=0;
		for(int i=0;i<fields.size();i++){
			if(fields.get(i).getStage() == 1){
				a1++;
			}
			else if(fields.get(i).getStage() == 2){
				a2++;
			}
			else if(fields.get(i).getStage() == 3){
				a3++;
			}
			else if(fields.get(i).getStage() == 4){
				a4++;
			}
		}
		s1 = new String[a1];
		for(int i=0,j=0;i<fields.size()&&j<a1;i++){
			if(fields.get(i).getStage() == 1){
				String t;
				if(member.getValue(fields.get(i).getChinese())== null){
					t = "无";
				}
				else{
					t = member.getValue(fields.get(i).getChinese());
				}
				String s = fields.get(i).getChinese()+":"+t;
				s1[j] = s;
				j++;
			}
		}
		if(process>=2){
			s2 = new String[a2];
			for(int i=0,j=0;i<fields.size()&&j<a2;i++){
				if(fields.get(i).getStage() == 2){
					String t;
					if(member.getValue(fields.get(i).getChinese())== null){
						t = "无";
					}
					else{
						t = member.getValue(fields.get(i).getChinese());
					}
					String s = fields.get(i).getChinese()+":"+t;
					s2[j] = s;
					j++;
				}
			}
		}
		if(process>=3){
			s3 = new String[a3];
			for(int i=0,j=0;i<fields.size()&&j<a3;i++){
				if(fields.get(i).getStage() == 3){
					String t;
					if(member.getValue(fields.get(i).getChinese())== null){
						t = "无";
					}
					else{
						t = member.getValue(fields.get(i).getChinese());
					}
					String s = fields.get(i).getChinese()+":"+t;
					s3[j] = s;
					j++;
				}
			}
		}
		if(process>=4){
			s4 = new String[a4];
			for(int i=0,j=0;i<fields.size()&&j<a4;i++){
				if(fields.get(i).getStage() == 4){
					String t;
					if(member.getValue(fields.get(i).getChinese())== null){
						t = "无";
					}
					else{
						t = member.getValue(fields.get(i).getChinese());
					}
					String s = fields.get(i).getChinese()+":"+t;
					s4[j] = s;
					j++;
				}
			}
		}
		
		return "showOfPerson";
	}
	
}
